home *** CD-ROM | disk | FTP | other *** search
- Path: informix.com!news
- From: Daniel Wood <dwood@informix.com>
- Newsgroups: comp.std.c
- Subject: Re: Undefined result vs. int's holding undefined values.
- Date: 8 Jan 1996 22:00:32 GMT
- Organization: Informix Software, Inc. Menlo Park, CA 94025
- Message-ID: <4cs460$d6e@news.informix.com>
- References: <4ck70b$rd7@news.informix.com> <4ckms5$rd7@news.informix.com> <4cmg0s$1mb@der.twinsun.com> <oZA8wQ9ytpjN084yn@csn.net>
- NNTP-Posting-Host: dwood.informix.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (X11; I; SunOS 5.4 sun4m)
- X-URL: news:oZA8wQ9ytpjN084yn@csn.net
-
- thads@csn.net (Thad Smith) wrote:
- >In article <4cmg0s$1mb@der.twinsun.com>,
- >eggert@twinsun.com (Paul Eggert) wrote:
- >>This reminds me of a similar bug I found a long time ago when porting
- >>the Modula-3 runtime, which contained code that acted something like this:
- >>
- >> int sum_overflow (int x, int y) {
- >> return (x + y < x) != (y < 0);
- >> }
- >>
- >>The C Standard does not guarantee that the above function works,
- >>since integer overflow leads to undefined behavior,
- >>but when I found that the function did not work with whatever old version
- >>of GCC I was using at the time, I reported it as a bug to the GCC maintainers
- >>and got a fix from them in a few days.
- >>
- >>Regardless of what the C Standard says, it should be obvious that it's
- >>crucial to have integer overflow checking working properly in an
- >>application that needs it.
- >
- >I agree, but it is possible to rewrite the function so that it doesn't
- >invoke undefined behavior:
- >
- > #include <limits.h>
- > int sum_overflow (int x, int y) {
- > return x > 0? (y > INT_MAX - x) : (y < INT_MIN - x);
- > }
- >
- >Thad
-
- I totally understand what you are doing in the above but this would have to
- be the ultimate in a cheap out for a vendor. SCO could claim that before
- ever looking at a test case containing a suspected compiler bug that every
- arithmetic calculation would have to first have a test similar to the above
- to protect against overflow/underflow. Does an appropriate "SAFE TEST" exist
- for multiple. Has anyone actually seen a real production program where every
- calculation was protected against overflow/underflow.
-
- Shame on SCO for using such a cheap out. There is no reason on an intel based
- platform not to be able to create an "IMPLEMENTATION DEFINED and consistant"
- behavior implementation instead of undefined behavior. Granted the standard
- doesn't require it but I have never seen a program with the kind of extra
- checking that seems to be required. Integer overflow/wraparound producing some
- specific defined behavior is easily "doable" on all machine architectures I
- know of even if the results might differ on different machines.
-
- Do any machines exist which actually explode when you add two number together
- such that the result would exceed MAXINT? :-) Get pratical!
-
- I am particularly interested in the answer to my "safe multiply" question
- above. It would be quite funny to find that there is actually no way to
- create, in a practical way, a safe c program that used multiple if the
- standard was followed to the letter of the law. I have thought of a way
- but it would be alot more involved then the sum_overflow() check above.
-
- --
- If you want a fancy saying then go find yourself a poet.
- If you want a bug cracked then you've come to the right place.
-
- "The numbers speak to me" - 44 61 6E 20 57 6F 6F 64
-
-